/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import Interaction.ServerConnection;
import java.rmi.RemoteException;
import Flightadministration.*;
import Data.*;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

/**
 *
 * @author David Ponce
 * @author Philipp Armbruster
 */
public class OverviewFlights extends JPanel {

    final Main td;
    private JPanel jp;
    public DefaultTableModel model;
    public JTable table;
    public Vector<Vector> rowData;
    public Vector<String> columnNames;
    final FlightRealizationCreate flrc;

    /*Das AdministrationsObjekt, welches über die getConnection methode vom RMI Server geholt wird, dem "admin" objekt zugewiesen.
    *Dadurch ist eine Kommunikation zwischen GUI und Applikationsserver mittels RMI möglich*/
    final Flightadministration admin = ServerConnection.getConnection();

    // Konstruktor
    public OverviewFlights(final Main td, final FlightRealizationCreate flrc) throws RemoteException {

        this.td = td;
        this.flrc = flrc;
        showOverviewFlights();
    }

    // Methoden
    public void showOverviewFlights() throws RemoteException {

        jp = new JPanel();

        //GridBagLayout
        //**********************************************************************

        GridBagLayout gbl = new GridBagLayout();
        jp.setLayout(gbl);
        jp.setBackground(Color.white);

        GridBagConstraints constraints = new GridBagConstraints();

        constraints.fill = GridBagConstraints.HORIZONTAL;   // wie Komponente Bereich füllen soll
        constraints.weightx = 1;                     // Breite
        constraints.weighty = 1;                    // Höhe
        constraints.insets = new Insets(0,0,1,1);   // Abstände definieren


        //Label
        //**********************************************************************

        JLabel headline = new JLabel("<html><h2 color=blue>Übersicht aller Flüge</h2></html>");
        headline.setHorizontalAlignment(JLabel.CENTER);
        constraints.gridx = 0;
        constraints.gridy = 1;
        constraints.gridwidth = 2;
        jp.add(headline, constraints);

        JLabel subheadline = new JLabel("<html><h3 color=grey><i>Bitte wählen Sie einen Flug aus und klicken Sie anschließend auf 'weiter'</i></h3></html>");
        subheadline.setHorizontalAlignment(JLabel.CENTER);
        constraints.gridx = 0;
        constraints.gridy = 2;
        constraints.gridwidth = 2;
        jp.add(subheadline, constraints);


        //JTable
        //**********************************************************************

        // Vector für Spaltenüberschrift mit Werten belegen
        columnNames = new Vector<String>();
        columnNames.add("ID");
        columnNames.add("Abflugtag");
        columnNames.add("Uhrzeit");
        columnNames.add("Ankunftstag");
        columnNames.add("Uhrzeit");
        columnNames.add("Abflughafen");
        columnNames.add("Zielflughafen");
        columnNames.add("Fluglinie");

        // Vector mit Daten für JTable belegen
        rowData = admin.getAllFlightsForTable();

        model = new DefaultTableModel( rowData, columnNames );
        model.addTableModelListener(table);

        table = new JTable(model);
        table.setModel(model);
        table.setAutoCreateRowSorter(true);
        table.getColumnModel().getColumn(0).setPreferredWidth(10);
        constraints.gridx = 0;
        constraints.gridy = 3;
        constraints.gridwidth = 3;
        table.setPreferredScrollableViewportSize(new Dimension(550,435));
        table.setVisible(true);
        jp.add( new JScrollPane(table), constraints);


        //Buttons
        //**********************************************************************

        JButton button_Next = new JButton("Weiter");
        constraints.gridwidth = 1;
        constraints.gridx = 0;
        constraints.gridy = 4;
        jp.add(button_Next, constraints);

        JButton button_Back = new JButton("Zurück");
        constraints.gridwidth = 1;
        constraints.gridx = 1;
        constraints.gridy = 4;
        jp.add(button_Back, constraints);


        //ActionListener
        //**********************************************************************

        button_Next.addActionListener(new ActionListener() {
                @Override
            public void actionPerformed(ActionEvent e){
                if (table.getSelectedRow()==-1) {
                    JOptionPane.showMessageDialog(null, "Bitte ein Element der Liste auswählen!");
                } else {
                    int selectedRow = table.getSelectedRow();
                        Object currentflightIdAsObject = table.getModel().getValueAt(selectedRow, 0);
                            int currentFlightId = Integer.parseInt(currentflightIdAsObject.toString());

                                try {

                                    Flight cF = admin.getFlightById(currentFlightId);

                                    // Vector mit allen Flughäfen aus der Datenbank befüllen
                                    final Vector<Airport> allAirports = admin.getAllAirportsForComboBox();
                                    // Über nachfolgende for-Schleife die Stadt des betroffenen Flughafens auslesen und in Stringvariable speichern
                                    String cityAndIntIdOfDepartureAirport = "";

                                        for(int i = 0; i<allAirports.size(); i++) {
                                            if(allAirports.get(i).getInternationalId().equals(cF.getDepartureAirportId())) {
                                                String portCity = allAirports.get(i).getPortCity();
                                                String intId = allAirports.get(i).getInternationalId();
                                                cityAndIntIdOfDepartureAirport = portCity + " (" + intId + ")";
                                            }
                                        }

                                    // Über nachfolgende for-Schleife die Stadt des betroffenen Flughafens auslesen und in Stringvariable speichern
                                    String cityAndIntIdOfArrivalAirport = "";

                                        for(int i = 0; i<allAirports.size(); i++) {
                                            if(allAirports.get(i).getInternationalId().equals(cF.getArrivalAirportId())) {
                                                String portCity = allAirports.get(i).getPortCity();
                                                String intId = allAirports.get(i).getInternationalId();
                                                cityAndIntIdOfArrivalAirport = portCity + " (" + intId + ")";
                                            }
                                        }

                                    // Vector mit allen Fluglinien aus der Datenbank befüllen
                                    final Vector<Airline> allAirlines = admin.getAllAirlinesForComboBox();
                                    // Über nachfolgende for-Schleife den Namen der betroffenen Fluglinie auslesen und in Stringvariable speichern
                                    String airlineName = "";

                                        for(int i = 0; i<allAirlines.size(); i++) {
                                            if(allAirlines.get(i).getAirId() == cF.getAirlineId()) {
                                                airlineName = allAirlines.get(i).getAirName();
                                            }
                                        }

                                    flrc.label3.setText(String.valueOf(cF.getFlightId()));

                                    flrc.label5.setText(cF.getTimeOfDeparture());

                                    flrc.label7.setText(cF.getTimeOfArrival());

                                    flrc.label9.setText(cityAndIntIdOfDepartureAirport);

                                    flrc.label11.setText(cityAndIntIdOfArrivalAirport);

                                    flrc.label13.setText(airlineName);

                                    flrc.label15.setText(cF.getDayOfDeparture());

                                } catch (RemoteException ex) {
                                    System.out.println(ex);
                                }

                    ((CardLayout) td.rightp.getLayout()).show(td.rightp, "FlightRealizationC");
                }
            }
        });


        button_Back.addActionListener(new ActionListener() {
                @Override
            public void actionPerformed(ActionEvent e){
                ((CardLayout) td.rightp.getLayout()).show(td.rightp,"FlightRealizationM");
            }
        });

    }
    public void updateTableData() throws RemoteException {

        // Vector mit Daten für JTable belegen
        rowData = admin.getAllFlightsForTable();

        // Tabelleninhalt wird aktualisiert
        model.setDataVector(rowData, columnNames);

    }
    public JPanel getJp() {
        return jp;
    }
}